package com.yanqu.road.server.cmd.variety;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.variety.VarietyProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.match.CrossMatchMgr;
import com.yanqu.road.server.manager.match.MatchWeekRankModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;

/**
 * @author ：zfr
 * @date ：2021/3/17 19:06
 */
@Cmd(code = CrossProtocol.C_VARIETY_GET_RANK_LIST, desc = "获取排行榜")
public class GetVarietyWeekRankListCmd extends CrossCommand {
    private long groupId;
    private int weekId;
    private boolean isGetReward;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        VarietyProto.GetWeekRankListCrossReqMsg msg = VarietyProto.GetWeekRankListCrossReqMsg.parseFrom(packet.getBytes());
        groupId = msg.getGroupId();
        weekId = msg.getWeekId();
        isGetReward = msg.getIsGetReward();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        VarietyProto.GetWeekRankListRespMsg.Builder respMsg = VarietyProto.GetWeekRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        respMsg.setValue("0");
        respMsg.setIsGetReward(isGetReward);
        MatchWeekRankModel rankListModel = CrossMatchMgr.getRankListModel(groupId, weekId);
        if (rankListModel != null) {
            RankProto.RankListMsg.Builder rankListMsg = rankListModel.getRankListMsg();
            respMsg.addAllRankList(rankListMsg.getRankListList());
            int myRank = rankListModel.getMyRank(packet.getPlayerId(), serverId);
            for(RankProto.RankTempMsg rankTempMsg : rankListMsg.getRankListList()) {
                if(rankTempMsg.getUserId() == packet.getPlayerId() && rankTempMsg.getPlayerBaseData().getServerId() == serverId){
                    respMsg.setValue(rankTempMsg.getValue());
                    break;
                }
            }
            respMsg.setMyRank(myRank);
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_VARIETY_GET_WEEK_RANK_LIST, respMsg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
